package com.hqd.utils.pack.complier;

import com.hqd.utils.pack.bean.ProjectInfo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;

public abstract class AbstractCompiler {
    public static final String FILE_SUFFIX_JAVA = ".java";
    public static final String FILE_SUFFIX_CLASS = ".class";
    public static final String FILE_SUFFIX_XML = ".xml";
    public static final String FILE_SUFFIX_PROPERTIES = ".properties";

    protected Logger logger = Logger.getLogger(this.getClass());
    protected ProjectInfo projectInfo;
    protected File projectPath;
    protected File srcPath;
    protected File testSrcPath;
    protected File outPath;

    public AbstractCompiler(ProjectInfo projectInfo) {
        this.projectInfo = projectInfo;
        check();
        this.projectPath = new File(projectInfo.getBasePath());
        this.srcPath = new File(projectPath, projectInfo.getSrcPath());
        this.testSrcPath = new File(projectPath, projectInfo.getTestSrcPath());
        this.outPath = new File(projectInfo.getOutPath());
    }

    protected void printTip(String param) {
        logger.error(String.format("参数 --%s 不能为空", param));
    }

    protected boolean check() {
        boolean isPass = true;
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isBlank(projectInfo.getBasePath())) {
            printTip("basePath(项目路径)");
            sb.append("basePath(项目路径) 参数为空 ");
            isPass = false;
        }
        if (StringUtils.isBlank(projectInfo.getJarPath())) {
            printTip("jarPath(目标jar包路径)");
            sb.append("jarPath(目标jar包路径) 参数为空 ");
            isPass = false;
        }
        if (projectInfo.getCompilerClass() == null) {
            printTip("compilerClass(编译器类)");
            sb.append("ompilerClass(编译器类) 参数为空 ");
            isPass = false;
        }
        if (!isPass) {
            throw new IllegalArgumentException(sb.toString());
        }
        return true;
    }

    public void compile() throws Exception {
        logger.debug("############ 编译开始 ############");
        logger.info(projectInfo);
        doCompile();
        logger.debug("############ 编译结束 ############");
    }


    /**
     * 复制资源文件
     *
     * @throws IOException
     */
    protected void addResources(File targetPath, File... files) throws IOException {
        logger.debug(String.format("\t###### 开始添加资源 ######"));
        if (ArrayUtils.isNotEmpty(files)) {
            String testResourcePath = new File(projectPath, projectInfo.getTestResourcePath()).getAbsolutePath();
            for (File resourceFile : files) {
                if (projectInfo.isSkipTest()) {
                    if (resourceFile.getAbsolutePath().startsWith(testResourcePath)) {
                        continue;
                    }
                }
                logger.info(String.format("\t\t###### 复制资源文件:%s ######", resourceFile));
                if (resourceFile.isFile()) {
                    FileUtils.copyFileToDirectory(resourceFile, targetPath);
                } else {
                    FileUtils.copyDirectoryToDirectory(resourceFile, targetPath);
                }
            }
        }
        logger.debug(String.format("\t###### 添加资源结束 ######"));
    }

    protected boolean isContainsDebugLevel(String level) {
        return StringUtils.contains(projectInfo.getDebugLevel(), level);
    }

    protected abstract void doCompile() throws Exception;
}
